package com.meng.listview_scrolldelete;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, MyAdapter.OnMenuClickListener {
    private ListView mListView;
    private MyAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
    }

    private void initViews() {
        mListView = (ListView) findViewById(R.id.listView);
        mAdapter = new MyAdapter(this);
        mAdapter.setDatas(getDatas());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        mAdapter.setOnMenuClickListener(this);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.e("MainActivity", "scrollState:" + scrollState);
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    private List<String> getDatas() {
        List<String> list = new ArrayList<>();
        for(int i = 1;i <= 50;i++){
            list.add("我是模拟数据---->"+i);
        }
        return list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTopClick(int position) {
        Toast.makeText(this, "置顶" + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
        mAdapter.toTop(position);
    }

    @Override
    public void onDeleteClick(int position,ItemLayout itemLayout) {
        Toast.makeText(this, "删除" + mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
        mAdapter.delete(position,itemLayout);

    }
}
